# coding: utf-8
#
# Copyright (c) 2020 zey <zeyio.com>
#
# MIT Licence. See http://opensource.org/licenses/MIT
#
# Created on 2020-04-09
#

import sys
import os
from workflow import Workflow3, web, ICON_WEB

from os import listdir
from os.path import isdir, join, expanduser

# 获取文件列表
def getFileList():
    args_list = wf.args
    result = []
    for path in args_list[1:]:
        # path = wf.args[1]
        log.debug('path: ' + path)
        path = expanduser(path)
        result.extend([{"file": f, "path": path} for f in listdir(path) if isdir(join(path, f))])
    return result

def main(wf):
    # 获取搜索参数
    searchKey = wf.args[0]
    log.debug('searchKey: ' + searchKey)
    # 文件列表缓存3s
    fileList = wf.cached_data('projects', getFileList, max_age=3)
    # 根据query过滤目录
    for item in fileList:
        if (searchKey and (searchKey in item.get('file'))):
            title = item.get('file')
            wf.add_item(title=title, subtitle=item.get('path'), arg=os.path.join(item.get('path'), title), valid=True);

    # 把应该展示的内容发送给Alfred
    wf.send_feedback()

if __name__ == '__main__':
    wf = Workflow3()
    log = wf.logger
    sys.exit(wf.run(main))
